package vcs.media.player;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.util.Log;

public class SongManager {

	
	public static int ARTIST = MediaMetadataRetriever.METADATA_KEY_ARTIST;
	public static int AUTHOR = MediaMetadataRetriever.METADATA_KEY_AUTHOR;
	public static int GENRE = MediaMetadataRetriever.METADATA_KEY_GENRE;
	public ArrayList<String> listNameArt = new ArrayList<String>();
	
	String art = "";
	
	// SDCard Path
		final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
		public static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
		private ArrayList<HashMap<String, String>> artistList = new ArrayList<HashMap<String,String>>();
		MediaMetadataRetriever media;
		ArrayList<String> listTemp = new ArrayList<String>();
		
		// Constructor
		public SongManager(){
			
		}
		
		/**
		 * Function to read all mp3 files from sdcard
		 * and store the details in ArrayList
		 * */
		public ArrayList<HashMap<String, String>> getPlayList(){
			
			System.out.println(MEDIA_PATH);
		    if (MEDIA_PATH != null) {
		        File home = new File(MEDIA_PATH);
		        File[] listFiles = home.listFiles();
		        if (listFiles != null && listFiles.length > 0) {
		            for (File file : listFiles) {
		                System.out.println(file.getAbsolutePath());
		                
		                if (file.isDirectory() && !file.getName().equals("Android") && !file.getName().equals("Ringtones") && 
		                		!file.getName().equals("notifications")) {
		                    scanDirectory(file);
		                } else {
		                    addSongToList(file);
		                }
		            }
		        }
		    }
		    // return songs list array
		    return songsList;
		}
		
		private void scanDirectory(File directory) {
		    if (directory != null) {
		        File[] listFiles = directory.listFiles();
		        if (listFiles != null && listFiles.length > 0) {
		            for (File file : listFiles) {
		            	
		            		
		                if (file.isDirectory()) {
		                    scanDirectory(file);
		                } else {
		                	
		                    addSongToList(file);
		                }

		            }
		        }
		    }
		}
		
		
		private void addSongToList(File song) {
			
		    if (song.getName().endsWith(".mp3")) {
		    	String nameSong = song.getName().substring(0, song.getName().length() - 4);
		    	String nameNotChar = removeExcess(nameSong);
		    	
		        HashMap<String, String> songMap = new HashMap<String, String>();
		        
		        // Adding each song to SongList
		        if(!songsList.isEmpty() && !compare(listTemp, song.getName())){
		        	listTemp.add(song.getName());
		        	songMap.put("songTitle",
		        			nameNotChar);
			        songMap.put("songPath", song.getPath());
			        Log.e("Thanh cong hay khong", String.valueOf(listTemp.size()));
			        
			        
		        	songsList.add(songMap);
		        }
		        
		        if(songsList.isEmpty()){
		        	listTemp.add(song.getName());
		        	
		        	songMap.put("songTitle",
		        			nameNotChar);
			        songMap.put("songPath", song.getPath());
			        
		        	songsList.add(songMap);
		        }
		    }
		}
		
		private void scanDirectory2(File directory, String nameArt, int key) {
		    if (directory != null) {
		        File[] listFiles = directory.listFiles();
		        if (listFiles != null && listFiles.length > 0) {
		            for (File file : listFiles) {
		            	
		            		
		                if (file.isDirectory()) {
		                    scanDirectory2(file, nameArt, key);
		                } else {
		                	
		                    addToArt(file, nameArt, key);
		                }

		            }
		        }
		    }
		}
		
		private void addToArt(File song,String nameArt, int key) {
			media = new MediaMetadataRetriever();
		    if (song.getName().endsWith(".mp3")) {
		    	String nameSong = song.getName().substring(0, song.getName().length() - 4);
		    	String nameNotChar = removeExcess(nameSong);
		    	Log.e("ttttttttttttttttttt", song.getPath());
		    	
		        HashMap<String, String> songMap = new HashMap<String, String>();
		        
		        media.setDataSource(song.getPath());
				art = media.extractMetadata(key);
				
				if(art == null){
					art = "Chua ro";
				}
				
				if(art.equalsIgnoreCase(nameArt)){
					// Adding each song to SongList
					Log.e("gggggggggggggggggggggggg", art);
			        if(!artistList.isEmpty() && !compare(listTemp, song.getName())){
			        	listTemp.add(song.getName());
			        	songMap.put("songTitle",
			        			nameNotChar);
				        songMap.put("songPath", song.getPath());
				        Log.e("Thanh cong hay khong", String.valueOf(listTemp.size()));
				        artistList.add(songMap);
			        }
			        
			        if(artistList.isEmpty()){
			        	listTemp.add(song.getName());
			        	
			        	songMap.put("songTitle",
			        			nameNotChar);
				        songMap.put("songPath", song.getPath());
				        
				        artistList.add(songMap);
			        }
				}
		    }
		}
		
		
		//Get artist
		public ArrayList<HashMap<String, String>> getSongFrom(String nameArt, int key){
			
			System.out.println(MEDIA_PATH);
		    if (MEDIA_PATH != null) {
		        File home = new File(MEDIA_PATH);
		        File[] listFiles = home.listFiles();
		        if (listFiles != null && listFiles.length > 0) {
		            for (File file : listFiles) {
		                System.out.println(file.getAbsolutePath());
		                
		                if (file.isDirectory() && !file.getName().equals("Android") && !file.getName().equals("Ringtones") && 
		                		!file.getName().equals("notifications")) {
		                    scanDirectory2(file, nameArt, key);
		                } else {
		                	addToArt(file, nameArt, key);
		                }
		            }
		        }
		    }
		    // return songs list array
		    return artistList;
		}
		
		/**
		 * Function to get all artist
		 * and store the details in ArrayList
		 * */
		public ArrayList<String> getContainMedia(int key){
//			ArrayList<String> temp = new ArrayList<String>();
//			ArrayList<String> temp2 = new ArrayList<String>();
//			File home = new File(MEDIA_PATH);
//			//File s = new file
//			media = new MediaMetadataRetriever();
//			String art = "";
//			
//			if (home.listFiles(new FileExtensionFilter()).length > 0) {
//				
//				for (File file : home.listFiles(new FileExtensionFilter())) {
//					media.setDataSource(file.getPath());
//					art = media.extractMetadata(key);
//					if(art == null){
//						art = "No name";
//					}
//					//if(art != null)
//					temp.add(art);
//				}
//			}
//			
//			for (int i = 0; i < temp.size(); i++) {
//				if(temp2.isEmpty()){
//					temp2.add(temp.get(i));
//				}else{
//					if(!compare(temp2, temp.get(i))){
//						temp2.add(temp.get(i));
//					}
//				}
//			}
//			// return songs list array
//			return temp2;
			media = new MediaMetadataRetriever();
			String name = "";
			ArrayList<HashMap<String, String>> arr = new ArrayList<HashMap<String,String>>();
			ArrayList<String> arr2 = new ArrayList<String>();
			ArrayList<String> arr3 = new ArrayList<String>();
			arr = songsList;//getPlayList();
			
			for(int i = 0; i < arr.size(); i++){
				media.setDataSource(arr.get(i).get("songPath"));
				name = media.extractMetadata(key);
				if(name == null){
					name = "Chua ro";
				}
				Log.e("aaaaaaaaaaaaaaaaaaaaaaaa", name);
				arr2.add(name);
			}
			
			for (int i = 0; i < arr2.size(); i++) {
				if(arr3.isEmpty()){
					arr3.add(arr2.get(i));
				}else{
					if(!compare(arr3, arr2.get(i))){
						arr3.add(arr2.get(i));
					}
				}
			}
			
			//temp = listNameArt;
			return arr3;
		}
		
		
		/**
		 * Function to compare name artist the same as
		 * and store the details in ArrayList
		 * */
		private boolean compare(ArrayList<String> arr, String str){
			for(int i = 0; i < arr.size(); i++){
				if(str.equalsIgnoreCase(arr.get(i))){
					return true;
				}
			}
			return false;
		}
		
		/**
		 * Function to compare name artist the same as
		 * */
		private String removeExcess(String str){
			char[] arr = str.toCharArray();
			String temp = "";
			for (char c : arr) {
				if(c == '_' || c == '-')
					break;
				
				if(c != '-' && c != '_')
					temp += c;
			}
			return temp;
		}
		
		/**
		 * Class to filter files which are having .mp3 extension
		 * */
		class FileExtensionFilter implements FilenameFilter {
			public boolean accept(File dir, String name) {
				return (name.endsWith(".mp3") || name.endsWith(".MP3"));
			}
		}
}
